/*
Kakine
ActionScript Framework
Copyright © 2011, PUNY, LLC.

This file is part of KAKINE.

KAKINE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

KAKINE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KAKINE.  If not, see <http://www.gnu.org/licenses/>.
*/ 
package com.puny.mvc.views.platformer
{
	import com.puny.mvc.views.platformer.movieclips.InvisiblePlatformView;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	
	public class MapGenerator extends Object {
		private var _view:MapView;
		private var _mc:MovieClip;
		private var _offset:Number;
		private var _procedure:Function;
		private var _children:int;
		public function MapGenerator(mapView:MapView,mc:MovieClip,offset:Number) {
			_view = mapView;
			_mc = mc;
			_children = _mc.numChildren;
			_offset = offset;
			_procedure = first;
		}
		
		public function rate():int {
			return 10 - _mc.numChildren * 10 / _children;
		}
		
		public function step():Boolean {
			_procedure();
			return _mc.numChildren > 0;
		}
		
		private function first():void {
			var child:DisplayObject
			var count:int = 3;
			for (var i:int = 0;i < _mc.numChildren;i++) {
				child = _mc.getChildAt(i);
				if (child is InvisiblePlatformView) {
					_mc.removeChild(child);
//					_view.copy(child,_offset);
					if (count == 0) {
						return;
					} else {
						count--;
					}
				}
			}
			_procedure = second;
		}
		
		private function second():void {
			var child:DisplayObject = _mc.getChildAt(_mc.numChildren-1);
			_mc.removeChild(child);
//			_view.copy(child,_offset);			
		}
		
	}
}